package com.xg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xg.common.constant.CommonStatusEnum;
import com.xg.common.result.ApiResult;
import com.xg.common.util.Func;
import com.xg.common.util.PageUtil;
import com.xg.common.util.PageUtilHelper;
import com.xg.common.util.PageUtils;
import com.xg.entity.UsedCar;
import com.xg.entity.UsedCarType;
import com.xg.mapper.UsedCarTypeMapper;
import com.xg.service.IUsedCarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jdh
 * @since 2023-03-30
 */
@Service
public class UsedCarTypeServiceImpl extends ServiceImpl<UsedCarTypeMapper, UsedCarType> implements IUsedCarTypeService {

    @Autowired
    private UsedCarTypeMapper usedCarTypeMapper;
    
    @Override
    public List<UsedCarType> queryUsedCarTypeList() {
        return usedCarTypeMapper.queryUsedCarTypeList();
    }

    @Override
    public PageUtil<UsedCarType> queryPage(Map<String, Object> params) {
        String typeName = (String) params.get("typeName");
        QueryWrapper<UsedCarType> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(typeName)){
            queryWrapper.like("type_name",typeName);
        }
        List<UsedCarType> list = usedCarTypeMapper.selectList(queryWrapper);
        PageUtil<UsedCarType> pageInfo = PageUtilHelper.pageHelper(list, Integer.parseInt((String) params.get("page")), Integer.parseInt((String) params.get("limit")));
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResult deleteType(List<Integer> ids) {
        int i = usedCarTypeMapper.deleteBatchIds(ids);
        if(i>0){
            return ApiResult.success();
        }else {
            return ApiResult.error(CommonStatusEnum.TYPE_DELETE_ERROR.getCode(), CommonStatusEnum.TYPE_DELETE_ERROR.getMsg());
        }
    }

    @Override
    public ApiResult<UsedCarType> selectTypeInfo(Integer typeId) {
        UsedCarType usedCarType = usedCarTypeMapper.selectById(typeId);
        if(!Func.isEmpty(usedCarType)){
            return ApiResult.success(usedCarType);
        }else {
            return ApiResult.error(CommonStatusEnum.TYPE_INFO_ERROR.getCode(), CommonStatusEnum.TYPE_INFO_ERROR.getMsg());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResult saveType(UsedCarType usedCarType) {
        int insert = 0;
        int update = 0;
        if (Func.isEmpty(usedCarType.getTypeId())) {
            insert = usedCarTypeMapper.insert(usedCarType);
        } else {
            update = usedCarTypeMapper.updateById(usedCarType);
        }
        if (insert>0||update>0) {
            return ApiResult.success();
        } else {
            return ApiResult.error(CommonStatusEnum.TYPE_SAVE_ERROR.getCode(), CommonStatusEnum.TYPE_SAVE_ERROR.getMsg());
        }
    }

    @Override
    public List<UsedCar> queryUsedCarTypeById(Integer typeId) {
        return usedCarTypeMapper.queryUsedCarTypeById(typeId);
    }
}
